package cn.com.bsfit.netty.task;

import cn.com.bsfit.netty.exception.DefaultException;
import cn.com.bsfit.netty.req.ReqData2MapStrategy;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 类名 HealthReq2DataTask
 * 描述 将消息转换为健康检查路由的Task(如果需要健康检查的话)
 *
 * @author hedonglin
 * @version 1.0
 * @date 2019/12/17 9:38
 */
public class HealthReq2DataTask implements Callable<Req2MapTaskRs> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private String msg;
    private ReqData2MapStrategy reqData2MapStrategy;

    public HealthReq2DataTask(String msg, ReqData2MapStrategy reqData2MapStrategy) {
        this.msg = msg;
        this.reqData2MapStrategy = reqData2MapStrategy;
    }

    @Override
    public Req2MapTaskRs call() throws Exception {
        try {
            Map<String, Object> map = reqData2MapStrategy.reqData2Map(msg);
            if (map == null || map.size() == 0){
                return new Req2MapTaskRs(false, false,false,false, "", Maps.newHashMap());
            }else {
                return new Req2MapTaskRs(true, false,false,true,"", map);
            }
        } catch (DefaultException e) {
            logger.warn(e.getMessage() == null ? "Socket数据为非健康检查" : e.getMessage());
        }

        return new Req2MapTaskRs(false, false,false,false, "", Maps.newHashMap());
    }
}
